<?php 

namespace Admin\Controller;

use Think\Controller;

class UserController extends Controller{
	 //创建用户
    public function create(){
    	$l = D('Login','Logic');
		$re = $l->islogin();
		if(!$re){
			redirect('/admin/login');
		}
		$admin = $_SESSION['user']['is_admin'];
        if($admin == 0){
            redirect('/admin/login');
        }
        $this->display('create');
    }

    public function submit(){
        $data['account'] = $_POST['account'];
        $data['name'] = $_POST['name'];
        $data['password'] = $_POST['password'];
        $repassword = $_POST['repassword'];
        $data['create_time'] = time();
        if(!is_mobile($data['account'])){
            redirect('/admin/user/create');
        }
        $length = strlen($data['password']);
        if($length<6 OR $length>12){
            redirect('/admin/user/create');
        }

        if($data['password'] != $repassword){
             redirect('/admin/user/create');
        }                       
        $s = D('Login','Logic');
        $re = $s->createUser($data);
        if(!$re){
            redirect('/admin/user/create');
        }else{
            redirect('/admin/user/lists');
        }
    }
    //用户列表
    public function lists(){
    	$l = D('Login','Logic');
		$re = $l->islogin();
		if(!$re){
			redirect('/admin/login');
		}

    	$s = D('User','Logic');
    	$re = $s->userList();
    	$this->assign('users',$re);
    	$this->display('list');
    }
    public function search_user(){
        $search = $_POST['Search'];
        if(is_mobile($search)){
            $data = array('account'=>$search);
        }else{
            $data = array('name'=>$search);
        }
        $s = D('User','Logic');
        $re = $s->searchUser($data);
        $this->assign('users',$re);
        $this->display('list');
    }
}